package android.slc.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.FileResource;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.log.Log;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class SlcFileResourceUtils {

    /**
     * 拷贝资源
     *
     * @param resourcePath
     * @param filePath
     */
    public static void copyResource(String resourcePath, String filePath) {
        copyResource(resourcePath, FileUtil.file(filePath));
    }

    /**
     * 拷贝资源
     *
     * @param resourcePath
     * @param targetPath
     */
    public static void copyResource(String resourcePath, File targetPath) {
        copyResource(ResourceUtil.getResourceObj(resourcePath), targetPath);
    }

    public static void copyResource(Resource resourceObj, File targetPath) {
        if (URLUtil.isJarURL(resourceObj.getUrl())) {
            JarFile jarFile = null;
            try {
                JarURLConnection urlConnection = (JarURLConnection) resourceObj.getUrl().openConnection();
                String basePath = urlConnection.getEntryName();
                jarFile = urlConnection.getJarFile();
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry currentEntry = entries.nextElement();
                    String currentEntryName = currentEntry.getName();
                    if (StringUtils.startsWith(currentEntryName, basePath)) {
                        if (!StringUtils.endsWith(currentEntryName, StrPool.SLASH)) {
                            try {
                                String entryRelativePathByBasePath = StrUtil.removePrefix(currentEntryName, basePath);
                                //String entryRelativePathByBasePath = StrUtil.removeAll(currentEntryName, basePath);
                                File dest;
                                if (StringUtils.isNotEmpty(entryRelativePathByBasePath)) {
                                    dest = FileUtil.file(targetPath, entryRelativePathByBasePath);
                                } else {
                                    dest = FileUtil.file(targetPath, FileNameUtil.getName(currentEntryName));
                                }
                                FileUtil.writeFromStream(ResourceUtil.getStream(currentEntryName), dest, true);
                            } catch (Exception e) {
                                Log.get().info("文件{}复制失败", currentEntryName);
                                Log.get().info("文件{}复制失原因：{}", currentEntryName, e);
                            }
                        } else {
                        }
                    }
                }
            } catch (IOException e) {
                throw new IORuntimeException(e);
            } finally {
                try {
                    if (jarFile != null) {
                        jarFile.close();
                    }
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
        } else {
            File resourceFile = getFileByResource(resourceObj);
            if (resourceFile == null) {
                return;
            }
            if (resourceFile.isDirectory()) {
                //Log.get().info("resourceFile：{}", resourceFile.getAbsolutePath());
            }
            FileUtil.copyContent(resourceFile, FileUtil.file(targetPath), true);
        }
    }

    public static File getFileByResource(String resource) {
        return getFileByResource(ResourceUtil.getResourceObj(resource));
    }

    public static File getFileByResource(Resource resourceObj) {
        File resourceFile = null;
        if (resourceObj instanceof ClassPathResource) {
            ClassPathResource classPathResource = (ClassPathResource) resourceObj;
            resourceFile = classPathResource.getFile();
        } else if (resourceObj instanceof FileResource) {
            FileResource fileResource = (FileResource) resourceObj;
            resourceFile = fileResource.getFile();
        }
        return resourceFile;
    }

}
